var e, t = require("../../../@babel/runtime/helpers/interopRequireDefault").default, a = require("../../../@babel/runtime/helpers/regeneratorRuntime"), n = require("../../../@babel/runtime/helpers/asyncToGenerator"), o = t(require("@vant/weapp/dialog/dialog.js")), i = t(require("../../../api/api_queryRemain")), r = t(require("../../../behaviors/auth")), s = require("../../../utils/util"), u = t(require("../../../libs/wxsubscribe/index")), c = getApp(), h = c.eventBus;

Component({
    behaviors: [ r.default ],
    properties: {
        hideSingleChargeBtn: {
            type: Boolean,
            default: !1
        },
        query: {
            type: Object,
            default: {}
        }
    },
    data: {
        pageShow: !0,
        apanage: "",
        phone: "",
        autoChargeBtnLoading: !1,
        selectedAmount: 100,
        amounts: [ {
            value: 20
        }, {
            value: 30
        }, {
            value: 50
        }, {
            value: 100
        }, {
            value: 300
        }, {
            value: 500
        } ]
    },
    observers: {
        phone: function() {
            var t = this;
            e || (e = (0, s.debounce)(function() {
                t.getApannage();
            }, 500)), e();
        }
    },
    methods: {
        init: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] && arguments[0], a = this.data.query || {}, n = a.phone, o = a.apanage, i = c.globalData, r = i.hasAuth, s = i.phone;
            (!r || !s && n) && (t = !0), this.getAuthUserInfo({
                needAuth: !0,
                forceUpdate: t
            }).then(function() {
                e.setData({
                    phone: n || c.globalData.phone || "",
                    apanage: o || ""
                });
            });
        },
        handleAmountClick: function(e) {
            var t = e.currentTarget.dataset.value;
            this.setData({
                selectedAmount: t
            });
        },
        subscribeTplMsg: function() {
            return new Promise(function(e) {
                new u.default({
                    env: "miniprogram",
                    cookie: c.globalData.token,
                    templateId: "Kbj2Be_BX0eyf32vYSu-n71pdVdRaGp4GxVYl7WrRSE",
                    seq: Date.now(),
                    action_id: "EFoh6j7GnB38EYCn",
                    scene: "",
                    extra_data: "",
                    success: function(t, a) {
                        console.log("success", t, a), e();
                    },
                    error: function(t) {
                        console.error(t), e();
                    }
                });
            });
        },
        onAutoChargeClick: function() {
            var e = this, t = this.data, a = t.query, n = t.phone;
            (0, s.testPhone)(n) ? a.ciphertext && a.associate ? o.default.confirm({
                context: this,
                message: "是否同意签约成功后立即发起一笔代扣？",
                confirmButtonText: "是",
                cancelButtonText: "否"
            }).then(function() {
                e.requestContract("1");
            }).catch(function() {
                e.requestContract("0");
            }) : this.requestContract() : o.default.alert({
                context: this,
                message: "请输入正确手机号"
            });
        },
        requestContract: function(e) {
            var t, r = this, s = this.data.selectedAmount, u = this.data.query || {}, c = u.stock_id, d = u.associate, l = u.ciphertext, p = u.openid, g = u.scence, f = u.nonce, m = this.data.phone;
            console.log(this.data.query);
            var v = "1" === e || "0" === e;
            this.setData({
                autoChargeBtnLoading: !0
            });
            var x, b = {
                money: s,
                phone: m,
                status: e,
                scence: g || "4",
                stockId: c,
                associate: d,
                ciphertext: l,
                openid: p,
                nonce: f
            };
            x = b, Object.keys(x).forEach(function(e) {
                return !x[e] && 0 !== x[e] && delete x[e];
            }), console.log(b), i.default.getContractMsg(b).then(function(e) {
                return (t = t || n(a().mark(function e(t) {
                    return a().wrap(function(e) {
                        for (;;) switch (e.prev = e.next) {
                          case 0:
                            if ("0" !== t.code) {
                                e.next = 7;
                                break;
                            }
                            return h.emit("toAutoChargeSuccessPage", {
                                phone: m,
                                value: s
                            }), e.next = 4, r.subscribeTplMsg();

                          case 4:
                            wx.navigateToMiniProgram({
                                appId: "wxbd687630cd02ce1d",
                                path: "/pages/index/index",
                                extraData: t.data.data
                            }), e.next = 8;
                            break;

                          case 7:
                            "3001" === t.code ? v ? o.default.confirm({
                                context: r,
                                message: "非王卡无法办理微信话费代扣",
                                confirmButtonText: "申请王卡"
                            }).then(function() {
                                wx.navigateToMiniProgram({
                                    appId: "wx55b926152a8c3bef",
                                    path: "pages/index/index?channel=01-2611-8042-9999"
                                });
                            }) : o.default.alert({
                                context: r,
                                message: "您是非王卡用户，无法办理自动充值业务"
                            }) : o.default.alert({
                                context: r,
                                message: t.message
                            });

                          case 8:
                            r.setData({
                                autoChargeBtnLoading: !1
                            });

                          case 9:
                          case "end":
                            return e.stop();
                        }
                    }, e);
                }))).apply(this, arguments);
            }).catch(function() {
                r.setData({
                    autoChargeBtnLoading: !1
                });
            });
        },
        afterBindgetphonenumber: function() {
            this.init(!0);
        },
        onBackClick: function() {
            var e = "pages/charge/index", t = getCurrentPages();
            h.emit("toChargePage", {
                phone: this.data.phone
            }), t[t.length - 2] && t[t.length - 2].route === e ? wx.navigateBack({
                delta: 1
            }) : wx.navigateTo({
                url: "/".concat(e)
            });
        },
        goToAutoChargeDocument: function() {
            wx.navigateTo({
                url: "/pages/webview/index?url=" + encodeURIComponent("https://txwk.10010.com/wkpen/view/AAe74E0f04.html")
            });
        },
        getApannage: function() {
            var e = this, t = this.data.phone;
            console.log(t), t && (0, s.testPhone)(t) && i.default.getApanage({
                phone: t
            }).then(function(t) {
                "0" === t.code && e.setData({
                    apanage: t.data
                });
            });
        },
        onPhoneInput: function(e) {
            var t = e.detail.value;
            this.setData({
                phone: t
            });
        }
    },
    created: function() {},
    attached: function() {
        this.init();
    },
    ready: function() {},
    moved: function() {},
    detached: function() {
        this.CompSub && this.CompSub.off();
    }
});